import axios from "axios";
// import {useStore} from 'vuex';
// const store=useStore()
const http=axios.create({
    // 设置请求根地址
    baseURL:'http://shop.bluej.cn/api',
    timeout:5000,
})

export const request=(url,data)=>{
    return new Promise((resolve,reject)=>{
        http.post(url,data,{
            headers:{
                "Content-Type":"application/json",
            }
        })
        .then(res=>{
            resolve(res)
        })
        .catch(err=>{
            reject(err)
        })
    })
}

export const requestPost=(url,token,data)=>{
    console.log(data);
    return new Promise((resolve,reject)=>{
        http.post(url,data,{
            headers:{
                "Content-Type":"application/json",
                'x-token':token
            }
        })
        .then(res=>{
            resolve(res)
        })
        .catch(err=>{
            reject(err)
        })
    })
}

export const requestGet=(url,token)=>{
    return new Promise((resolve,reject)=>{
        http.get(url,{
            headers:{
                "Content-Type":"application/json",
                'x-token':token
            }
        })
        .then(res=>{
            resolve(res)
        })
        .catch(err=>{
            reject(err)
        })
    })
}

export const requestGetNT=(url,params)=>{
    return new Promise((resolve,reject)=>{
        http.get(url,{params},{
            headers:{
                "Content-Type":"application/json",
            }
        })
        .then(res=>{
            resolve(res)
        })
        .catch(err=>{
            reject(err)
        })
    })
}

export const requestPut=(url,token,data)=>{
    return new Promise((resolve,reject)=>{
        http.put(url,data,{
            headers:{
                "Content-Type":"application/json",
                'x-token':token
            }
        })
        .then(res=>{
            resolve(res)
        })
        .catch(err=>{
            reject(err)
        })
    })
}

export const requestDel=(url,token)=>{
    return new Promise((resolve,reject)=>{
        http.delete(url,{
            headers:{
                "Content-Type":"application/json",
                'x-token':token
            }
        })
        .then(res=>{
            resolve(res)
        })
        .catch(err=>{
            reject(err)
        })
    })
}

export const upLoader=(url,data)=>{
    return new Promise((resolve,reject)=>{
        http.post(url,data,{
            headers:{
                "Content-Type":"multipart/form-data",
            }
        })
        .then(res=>{
            resolve(res)
        })
        .catch(err=>{
            reject(err)
        })
    })
}